#/usr/bin/perl
#Any questions, email:gaoxinglong9999@163.com
use strict;
use warnings;


#!/bin/perl 
use warnings;
use strict;
use File::Basename;
use File::Path;

my $src_root = $ARGV[0];
my $src_flist = $ARGV[1];
my $dst_root = $ARGV[2];
my $dst_bits = 16;
my $dst_sample_rate = 16000;

if (@ARGV<3) {
	print "There is not enough parameter !\r\n";
	print "Usage : $0 <original wave root dir> <part list> <output wave root dir>\n";
	exit(-1);
}

my $isUnix = 0;
my $dirsep = "\\";
if($isUnix){
	$dirsep = "\/";
}

if (@ARGV<3) {
	print "error\r\n";
	exit(-1);
}


open LIST, "<$src_flist" or die "$!";
my @files = <LIST>;
chomp @files;
close LIST;

my $succ_num = 0;
foreach my $afile (@files)
{
	my $dirname = dirname($afile);
	my $outputfulldir = $dst_root.$dirsep.$dirname;
	if(! -e $outputfulldir){
		File::Path::make_path($outputfulldir)
	}
	my $inputfullfile = $src_root.$dirsep.$afile;
	my $outputfullfile = $dst_root.$dirsep.$afile;
	if (! $isUnix) {
		my $exchangeCmd = "sox.exe -b 16 -r 8000 $inputfullfile -b $dst_bits -r $dst_sample_rate  $outputfullfile";
		my $exeresult = `$exchangeCmd`;
		if ($exeresult ne "") {
			print "exchange error \r\n";
			
		}
	}else {
		my $exchangeCmd = "sox.exe -b 16 -r 8000 $inputfullfile -b $dst_bits -r $dst_sample_rate  $outputfullfile";
		my $exeresult = `$exchangeCmd`;
		if ($exeresult ne "") {
			print "exchange error \r\n";
			
		}
	}
	$succ_num++;
	if ($succ_num % 1000 == 0) {
		print $succ_num."  processed \n";
	}
	
	
}

